<?php 
define("CCTRL", "category");
define("CCTRLNAME", "category");
define("TOKEN", "63788bc021be1634cda7a2ae432cf30b");
session_start();

class category extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       //private $colModel = array("ID","Name","Level","Status","Insert","Update","Delete");
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('smarty3');
            $this->load->model('APanel/category_model');
            $this->load->model('colmod/category_colmod');
            $this->load->model('APanel/my_model');
            include APPPATH . 'libraries/defu.php';
            if(!isset($_SESSION["ADP"][CCTRL]["display"]))$_SESSION["ADP"][CCTRL]["display"]=0;
            if(!isset($_SESSION["ADP"][CCTRL]["pageSize"]))$_SESSION["ADP"][CCTRL]["pageSize"]=100;
            $this->smarty=new CI_Smarty3();$this->smarty->error_reporting = E_ALL & ~E_NOTICE;
            $this->category_model=new category_model();
            $this->colModel=new category_colmod();
            $this->my_model=new my_model("dtb_category");
        }
        function querybuil(){
            print_r($_SESSION["debug"]["SQLquery"]);
        }
        public function index()
	{
            //unset($_SESSION["ADP"][CCTRL."-JQX"]["colModel"]);
            if(!checkAuthority(CCTRLNAME)){
                $this->smarty
                        ->assign('_SESSION', $_SESSION)
                        ->display("sys/04_access_denied");
                return;
            }
            if(isset($_SESSION["ADP"][CCTRL."-JQX"]["colModel"]))
                $colModel=$_SESSION["ADP"][CCTRL."-JQX"]["colModel"];
            else{
                $colModel=$this->colModel->get();
                $_SESSION["ADP"][CCTRL."-JQX"]["colModel"]=$colModel;
            }
            $Data["colModel"]=$colModel;
            $Data["display"]=$_SESSION["ADP"][CCTRL]["display"];
            //$Data["pageSize"]=$_SESSION["ADP"][CCTRL]["pageSize"];
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("APanel/".CCTRL."/".CCTRL);
	}
        function Edit($ID=""){
            $Data=null;
            if($ID!=""){
                $pr=$this->my_model->get($ID);
                if(count($pr)>0)$Data["OBJ"]=objectToArray($pr[0]);
            }
            
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("APanel/".CCTRL."/02_edit");
        }
        function Save(){
            $msgs=array();
            $name=$this->input->post("Name");
            if( $name===false || $name==""){
                $msgs[]="[Should be non-empty] Tên không được để trống.";
            }
            $alias=converturl($name);
            if( $alias==""){
                $msgs[]="[Should be non-empty] Alias không được để trống.";
            }
            if(count($msgs)>0){
                $code=-44;
                $msg="";
                foreach ($msgs as $m){
                    $msg.="$m<br/>";
                }
            }else{
                $Params=array(
                    "alias"=> $alias,
                    "name"=> $name,
                    "level"=> (int)$this->input->post("Level"),
                    "note"=> $this->input->post("Note")
                    
                );
                
                if(!checkAuthority(CCTRL,false)){
                    $code=-1;
                    $msg="[Access Denied] Bạn không có quyền thực hiện thao tác này.<br/> Mọi thay đổi dữ liệu đều không được công nhận.";
                }elseif(isset($_POST["ID"]) && $_POST["ID"]!=""){
                    if($this->my_model->update($_POST["ID"],$Params)){
                        $code=1;
                        $msg="[Success] Thành công. Dữ liệu đã được cập nhật.";
                    }else{
                        $code=-1;
                        $msg="[Fail] Thất bại. Vui lòng kiểm tra dữ liệu và thử lại.";
                    }
                }else{
                    $Params["status"]="false";
                    if($this->my_model->insert($Params)){
                        $code=1;
                        $msg="[Success] Thành công. Dữ liệu đã được thêm.";
                    }else{
                        $code=-1;
                        $msg="[Fail] Thất bại. Vui lòng kiểm tra dữ liệu và thử lại.";
                    }
                }
                //echo"<pre>";print_r($VideoParams);echo"</pre>";return;
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function jqgrid_data(){
            $colModel=$this->colModel->get();
            $orders=array();
            $result['total_rows']=0;
            //if($this->checkauthority()>=0){
                $result=$this->category_model->binding();
                $rows=$result['rows'];
                // get data and store in a json array
                foreach ($rows as $row) {
                    $cel=array();
                    foreach ($colModel as $col){
                        if(is_array($col)){
                            $f=$col["datafield"];
                            $cel[$f]=$row->$f;
                        }else{
                            $cel[$col]=$row->$col;
                        }
                    }
                    $cel['Status']=($row->Status=="true" || $row->Status=="Public")?true:false;
                    $orders[] = $cel;
                }
            
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $orders
            );
            echo json_encode($data);
        }
        
        
        
        
       
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */